/**
 * Created by kedong on 2016/5/17.
 */
window.onload = function () {
    /**
     * 弹窗的取消、确认事件
     */
    var pop = Pop.init({
        debug: true,
        confirm: function () {
            // 清空购物车
            $(".J_goods_list_item_cantainer").find(".row:not(.J_goods_list_item_template)").remove();
            // 清空购物车数据
            YQ.car.clear();
        }
    });

    /**
     *  购物车清空按钮点击事件
     */
    $(".clear_car span").tap(function (e) {
        e.stopPropagation();
        pop.alert();
    });

    /**
     * 获取列表数据
     */
    YQ.ajax({
        url: YQ.url,
        data: {
            msgId: "GET_GOODS_LIST",
            activityId: YQ.getWithOutJson("activityId")
        },
        success: function (data) {
            RenderList.render(data.obj);
        }
    });

    /**
     * 获取爽约状态（下单权限）
     */
    YQ.ajax({
        url: YQ.url,
        data: {
            msgId: "GET_ORDER_RIGHTS",
            userId: YQ.get("user")["id"]
        },
        success: function (data) {

        }
    });
};

/**
 * 生成商品列表
 */
(function ($) {
    var RenderList = function (data) {
        this.data = data || [];
        this.template = $(".J_food_item_template");
        this.cantainer = $(".J_food_list_cantainer");

        if (data.length > 0) {
            YQ.log.info(data);
            for (var i = 0, count = data.length; i < count; i++) {
                this.renderList(data[i]);
            }
        }
    };

    RenderList.prototype = {
        renderList: function (data) {
            /**
             * 初始化变量
             */
            var new_item = this.template.clone(), J_img, J_stock, J_food_name, J_price, J_href, href, J_action,
                info = {
                    id: data.id,
                    img: data.imgUrl,
                    name: data.name,
                    stock: data.stock,
                    price: data.price
                };

            $(new_item).removeClass("J_food_item_template hidden");
            /**
             * 获取对象
             */
            J_img = $(new_item).find(".J_img img");
            J_stock = $(new_item).find(".J_stock");
            J_food_name = $(new_item).find(".J_food_name");
            J_price = $(new_item).find(".J_price");
            J_href = $(new_item).find(".J_href");
            J_action = $(new_item).find(".J_action");
            /**
             * 设置数据
             */
            $(J_img).attr("src", YQ.resource + info.img);
            $(J_stock).text(info.stock).attr("data_stock", info.stock);
            $(J_food_name).text(info.name).attr("data_name", info.name);
            $(J_price).text(info.price).attr("data_price", info.price);

            /**
             *  添加  "J_action_" + X 的class是为了在点击加号和减号做同步操作时能够 快速的准确选择到相应的元素
             *
             *  另：也作为用以判断某一个元素是否存在于列表中的依据
             */
            $(J_action).addClass("J_action_" + info.id).attr("data_goodsId", info.id);
            $(J_action).data("goods", data);

            // 设置条状连接
            href = "foodDetail.html?id=" + info.id;
            $(J_href).tap(function () {
                location.href = href;
            });
            this.cantainer.append(new_item);
        }
    };

    RenderList.render = function (data) {
        if (data) {
            new this(data);
        }
    };

    window["RenderList"] = RenderList;
})(Zepto);
